package com.bridgeintelligent.tag.engine.parse;

import com.bridgeintelligent.tag.engine.api.DimInfo;
import com.bridgeintelligent.tag.engine.api.DimItemInfo;
import com.bridgeintelligent.tag.engine.api.EngineConfig;
import com.bridgeintelligent.tag.engine.core.cache.CacheConfig;
import com.bridgeintelligent.tag.engine.core.cache.CacheUtils;
import com.bridgeintelligent.tag.utils.FieldHandler;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by JackCheung on 2019/10/23 12:56 AM.
 */
@Slf4j
public class BaseParse {

    public static String GROUP_FORMAT = "%s_%s";
    private static final String SUB_FLAG = "_";
    private static final String SHOW_VALUE_AND_ORIGINAL_NAME = "标签值中文名+标签值";
    private static final String SHOW_VALUE_AND_ORIGINAL_VALUE = "05";
    /**
     * 查询缓存，把编号转换为实际的值
     *
     * @param fieldsSet fieldsSet
     * @param sourceMap sourceMap
     * @param tempMap   tempMap
     */
    static void commonParse(Collection<String> fieldsSet, Map<String, Object> sourceMap, Map<String, String> tempMap,String dataDate) {
        fieldsSet.forEach(k -> {
            Object v = sourceMap.get(k);
            //获取真实k
            k = FieldHandler.reduceSuffixField(k,dataDate);
            //如果对应标签有对应item值，则从缓存中取出该值
            if (null != v) {
                if ( StringUtils.equalsIgnoreCase( k, EngineConfig.CUST_NO ) ) {
                    tempMap.put(k, v.toString());
                } else {
                    //从缓存中取出标签名
                    DimInfo dimInfo = CacheUtils.getCacheInfo(CacheConfig.DIM_INFO, k);
                    String dimName = Optional.ofNullable(dimInfo)
                                             .map(DimInfo::getDimName)
                                             .orElse(k);

                    String showType = Optional.ofNullable(dimInfo)
                                              .map(DimInfo::getDimeShowType)
                                              .orElse("03");//如果为空则showType为03
                    if(v instanceof List){
                        List<Object> values = (List<Object>) v;
                        String finalK = k;
                        String result = values.stream().map(value->{
                            //从缓存中取出条目名
                            String itemName = Optional.ofNullable((DimItemInfo) CacheUtils.getCacheInfo(CacheConfig.DIM_ITEM_INFO, String.format(GROUP_FORMAT, finalK, value)))
                                    .map(DimItemInfo::getDimItemName)
                                    .orElse(value.toString());
                            if (SHOW_VALUE_AND_ORIGINAL_VALUE.equalsIgnoreCase(showType)||SHOW_VALUE_AND_ORIGINAL_NAME.equalsIgnoreCase(showType)) {
                                return String.format(GROUP_FORMAT, itemName, value);
                            } else {
                                return StringUtils.isEmpty(itemName) ? value.toString() : itemName;
                            }
                        }).collect(Collectors.joining(","));
                        tempMap.put(StringUtils.isEmpty(dimName) ? k : dimName, result);
                    } else {
                        //从缓存中取出条目名
                        String itemName = Optional.ofNullable((DimItemInfo) CacheUtils.getCacheInfo(CacheConfig.DIM_ITEM_INFO, String.format(GROUP_FORMAT, k, v)))
                                .map(DimItemInfo::getDimItemName)
                                .orElse(v.toString());
                        if (SHOW_VALUE_AND_ORIGINAL_VALUE.equalsIgnoreCase(showType)||SHOW_VALUE_AND_ORIGINAL_NAME.equalsIgnoreCase(showType)) {
                            tempMap.put(StringUtils.isEmpty(dimName) ? k : dimName, String.format(GROUP_FORMAT, itemName, v.toString()));
                        } else {
                            tempMap.put(StringUtils.isEmpty(dimName) ? k : dimName, StringUtils.isEmpty(itemName) ? v.toString() : itemName);
                        }
                    }
                }
            } else {//如果对应标签没有对应item值，则默认itemName为-
                if (EngineConfig.CUST_NO.equalsIgnoreCase(k)) {
                    tempMap.put(k, "-");
                } else {
                    String dimName = Optional.ofNullable((DimInfo) CacheUtils.getCacheInfo(CacheConfig.DIM_INFO, k))
                            .map(DimInfo::getDimName)
                            .orElse(k);
                    tempMap.put(StringUtils.isEmpty(dimName) ? k : dimName, "-");
                }
            }
        });
    }

    protected static String fetchRealKey(String k,String symbols) {
        String latest = StringUtils.substringAfterLast(k, SUB_FLAG);
        if (symbols.contains(latest)) {
            return StringUtils.substringBeforeLast(k, SUB_FLAG);
        } else {
            return k;
        }
    }
}
